<?php
/**
 * 學生學期缺曠課
 * @author cik
 *
 */
class Default_Model_Upgrade_StudAbsent extends Default_Model_Upgrade_Core
{
    protected $_table = 'stud_absent';

    protected $_targetTable = array('student_absence');

    function upgrade (){
        if ($this->isUpgrade())
        return 'isUpgrde';
        else {
            $query = "SELECT COUNT(*)  AS cc FROM student_absence";
            $res = $this->_newdbPdo->query($query)->fetch();
            $hasData = $res['cc'];

            $query = 'SELECT a.*,b.student_sn FROM stud_absent a, stud_base b WHERE a.stud_id=b.stud_id ';
            $res = $this->_sfs3Pdo->query($query)->fetchAll(PDO::FETCH_ASSOC);
            foreach($res as $row) {
                $arr = array();
                $arr['year'] = $row['year'];
                $arr['semester'] = $row['semester'];
                $temp = explode("_",$row['class_id']);
                $arr['grade'] = (int)$temp[2];
                $arr['class_sn'] = (int) $temp[3];
                $arr['student_sn'] = $row['student_sn'];
                $arr['date'] = $row['date'];
                //$arr['section'] = ($row['section']=='allday')?0: $row['section'];
                $arr['section'] = $row['section'];
                $arr['teacher_sn'] = $row['sign_man_sn'];
                $arr['sign_time'] = $row['sign_time'];
                $arr['kind'] = $this->iconv($row['absent_kind']);
                if ($hasData) {
                $find = $this->_newdbPdo->query("SELECT student_sn FROM student_absence
				  WHERE student_sn ='{$arr['student_sn']}'
				  AND year ='{$arr['year']}'
				  AND semester ='{$arr['semester']}'
				  AND grade ='{$arr['grade']}'
				   AND kind ='{$arr['kind']}'
				  ");
                if ($find->rowCount() > 0)
                continue;
                }
//                $this->newdb->insert('student_absence',$arr);
                $this->fromArray('student_absence',$arr);
            }
            $log['memo'] = '學生學期出缺席資料';
            $log['upgrade_time']	= date("Y-m-d h:i:s");

            $this->finishUpgrade($log);

        }
    }
}